<?php


namespace app\auth\service;


use app\common\models\AdminModel;

class LoginService
{
    /**
     * @return array
     */
    public static function login($userName,$pwd,$code) :array
    {
        // 检测验证码是否正确
        if(!captcha_check($code)) {
            return ['code' => 3,'msg' => '验证码不正确'];
        }

        $where = [
            'user_name' => $userName,
            'status' => 1
        ];
        $adminModel = new AdminModel();
        $admin = $adminModel->where($where)->find();
        if(!$admin) {
            return ['code' => 1,'msg' => '该用户不存在!'];
        }else {
            if(password_verify($pwd,$admin['password'])) {
                // 获取用户的角色
                $adminUserService = new AdminUserService();
                $group = $adminUserService->getUserGroup($admin['id']);
                $admin['group'] = $group;
                session('admin',$admin);
                return ['code' => 0,'msg' => '登录成功'];
            }else {
                return ['code' => 1,'msg' => '用户名或密码不正确'];
            }
        }
    }

}